package s.k.gui.webtest.page;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import s.k.gui.webtest.Allocator;
import s.k.gui.webtest.BrowserWindow;
import s.k.gui.webtest.WebDriverBrowserWindow;
import s.k.gui.webtest.constvalue.ConfigConstValue;
import s.k.gui.webtest.element.locator.Locator;
import s.k.gui.webtest.element.locator.LocatorChain;
import s.k.gui.webtest.element.tools.WebDriverElementExist;
import s.k.gui.webtest.element.tools.WebElementExist;
import s.k.gui.webtest.log.MyLogger;

public class Frame extends Page {

	private WebElementExist exist;
	
	protected  Page parent;
	
	public void setParent(Page parent){
		
		this.parent =parent;
	}

	
	@SuppressWarnings("unused")
	private boolean selected;

	protected String fullName = "";

	public void setFullName(String fullName) {

		this.fullName = fullName;

	}

	private WebElement element;

	public WebElement getElement() {

		return element;

	}

	public void setElement(WebElement element) {

		this.element = element;
	}


	private Locator locator = new Locator();


	private static MyLogger logger = MyLogger.getLogger(Frame.class);

	public Locator getLocator() {

		return locator;

	}

	public void setLocator(Locator locator) {

		this.locator = locator;
	}

	public Frame(String comment) {

		super();

		this.setSelected(false);

		this.comment = comment;

		this.init();

	}

	public void addIDlocator(String id) {

		this.locator.addIdLocator(id);
	}

	public void addNameLocator(String name) {

		this.locator.addNameLocator(name);

	}

	public void addXpathLocator(String xpath) {

		this.locator.addXpathLocator(xpath);
	}

	public void addCSSLocator(String css) {

		this.locator.addCSSLocator(css);
	}

	public void addLinkTextLocator(String linkText) {

		this.locator.addLinkTextLocator(linkText);

	}

	public void addPartialLinkTextLocator(String partialLinkText) {

		this.locator.addPartialLinkTextLocator(partialLinkText);

	}

	public void addAttributeLocator(String attributeName, String attributeValue) {

		this.locator.addAttributeLocator(attributeName, attributeValue);

	}

	private LocatorChain infoChain = new LocatorChain();

	private String currentLocator;

	public void setCurrentLocator(String currentLocator) {

		this.currentLocator = currentLocator;
	}

	public String getCurrentLocator() {

		return currentLocator;
	}

	public LocatorChain getInfoChain() {

		return infoChain;
	}

	private boolean isFrameExist(int waitCount) {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			exist = new WebDriverElementExist(this);

		}

		return exist.isElementExist(waitCount);
	}


	public boolean isExist() {

		return isFrameExist(ConfigConstValue.defaultWaitCount);

	}

	public void selectFrame() {

		StringBuilder message = new StringBuilder();

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (isExist()) {

			message.append(this.comment + ":");

			if (currentWindow instanceof WebDriverBrowserWindow) {

				try {
					if (locator.getLocatorById() != null)

						((WebDriver) currentWindow.getDriver().getEngine())
								.switchTo().frame(locator.getLocatorById());

					else if (locator.getLocatorByName() != null) {

						((WebDriver) currentWindow.getDriver().getEngine()).switchTo().frame(locator.getLocatorByName());

					} else {

						((WebDriver) currentWindow.getDriver().getEngine()).switchTo().frame(element);

					}

					message.append("定位Frame成功!现在的控制权交予这个Frame!");
					
					logger.info(message);
					
					setSelected(true);
					
					parent.setSelected(false);

				} catch (Exception e) {
					
					setSelected(false);

					message.append("定位Frame失败，没有找到这个Frame!");

					logger.error(message);
				}
			}
		}else{
			
			logger.error(message);
			
			setSelected(false);
		}
	}

	/**
	 * 选择最上层的页面
	 * */
	public void selectTopPage() {

		StringBuilder message = new StringBuilder(this.comment + ":");

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			((WebDriver) currentWindow.getDriver().getEngine()).switchTo().defaultContent();
			
			message.append("控制权在默认的window下");
			
			logger.info(message);
		}
	}
	
	public void select(){
		
		if(!isSelected()){
			
			selectTopPage();
			
			for(int i=1;i<infoChain.getInfoNodes().size();i++){
				
				((Frame)infoChain.getInfoNodes().get(i)).selectFrame();
			}
		}
	}
	
//	public void selectParentContainer(){
//		
//		StringBuilder message=new StringBuilder(this.comment+":");
//		
//		BrowserWindow currentWindow=Allocator.getInstance().currentWindow;
//		
//		if(currentWindow instanceof WebDriverBrowserWindow){
//			
//		}
//	}

	/**等待SubPage出现，失败打错误日志，程序继续运行<br>
	 * 默认等待时间是30s
	 * */
	public boolean waitForExist() {

		if (!isFrameExist(ConfigConstValue.waitCount)) {

			logger.error("元素" + comment + "一直没有出现");

			return false;

		}
		return true;
	}

	/**等待SubPage消失,失败打错误日志，程序一直运行*/
	public void waitForDisppear() {

		int waitCount = 0;

		while (waitCount++ < ConfigConstValue.waitCount) {

			if (!exist.isElementExistForCheckOnce())

				return;
		}
	}

}
